
*************************************************************************
*																		*
*					Campaign Panel Book Chapter 5 - Operationalization of Turnout in Campaign Panel 09			*
*																		*
*************************************************************************	




*******************************************************************************
** I.1 Generate Turnout-Variables 
*******************************************************************************   

fre kp1_170 kp2_170 kp3_170 kp4_170 kp5_170 kp6_170 kp7_180


* Value range from 0 to 1 (Postal voters = 1)
foreach welle of numlist 1/6 {
recode kp`welle'_170 (1 6=1) (2=0.75) (3=0.5) (4=0.25) (5=0) (98=0.5) (else=.), gen(wwbt`welle')  
   }
   
tab kp3_170 wwbt3, m
   
gen wwbt7=1 if kp7_180==1 | kp7_180==6
replace wwbt7=0 if kp7_180==2 

tab wwbt7 kp7_180, m

* Integrate postal voters: after postal vote constant to 1
foreach welle of numlist 4/7 {  
replace wwbt`welle' = 1 if kp3_170==6
}

foreach welle of numlist 5/7 {  
replace wwbt`welle' = 1 if kp4_170==6
}

foreach welle of numlist 6/7 {  
replace wwbt`welle' = 1 if kp5_170==6
}
   
foreach welle of numlist 7/7 {  
replace wwbt`welle' = 1 if kp6_170==6
}   
   
   
fre wwbt?   


*******************************************************************************
** I.2 Generate variable which indicates whether a respondent possessed a stable
* turnout intention throughout the campaign panel 
*******************************************************************************

egen sdwwbt = rowsd(wwbt?)
gen stable = 0 if sdwwbt!=.
replace stable = 1 if sdwwbt==0
fre stable  


**********************************************************
** Case Selection
***********************************************************

* two valid observations for turnout intention
egen misswbt = rowmiss(wwbt?)

gen twovalid = 0
replace twovalid = 1 if misswbt<=5  //minimum two valid observations


lab var twovalid "Zwei gültige Beobachtungen Wahlbeteiligungsabsicht"
fre twovalid

* 7 valid observations
gen allvalid = 0
replace allvalid = 1 if misswbt==0 | (misswbt==1 & w1==0)

fre allvalid
lab var allvalid "7 gültige Beobachtungen Wahlbeteiligungsabsicht"

tab allvalid w1, m


****************************************************************************
** Dynamic Types Turnout
*****************************************************************************

/* Increasing and decreasing trends, the code is probably 
valid only for respondents with 7 valid observations
*/

/* We do not need main diagonal percentages. To figure out whether observations 
are perfectly stable or not we can use the variable "stable", which was calculated
via rwosd. This variable has the advantage of being able to deal with 
complex missing patterns. Much more efficient. */

* Create ID-Variables for intraindividual regression coefficients
egen id = group(lfdn) if twovalid==1	//minimum two valid observations

fre id if twovalid==0

* Reduce size of dataset to include relevant variables only
keep lfdn wwbt* stable twovalid allvalid id


* Transform to long format
reshape long wwbt, i(lfdn) j(welle)

* Create target variable for coefficients of intraindividual regression
gen rohkoeff = . 

* Loop over all respondents to calculate regression: 
* Fill target variable with values
sum id
forvalues num=1/`r(max)' {	
quietly reg wwbt welle ///
	if id==`num'
replace rohkoeff = _b[welle] ///
	if id==`num'
}

sum rohkoeff

* Transform back into wide format and generate types
reshape wide

* Types
lab define typ ///
	-1 "decreasing" ///
	0 "stable" ///
	1 "increasing", replace
	
tab rohkoeff stable, m	

gen wwbttyp = -1 if rohkoeff<0 & stable==0
replace wwbttyp = 0 if rohkoeff==0 & stable==1
replace wwbttyp = 1 if rohkoeff>0 & stable==0


lab val wwbttyp typ

fre wwbttyp	//526 missings fluctuation + less than two observations

lab var wwbttyp "Individual trend turnout"

*---------------------------------------
* Save
*----------------------------------------

drop id rohkoeff 
	
save "${path}\Reproducible Do-Files\Chapter 5\Data\mobilization09.dta", replace	
